package main

import (
	"noip_updator/common"
	"noip_updator/server"
	"noip_updator/service"
	"os"
	"path/filepath"

	"github.com/spf13/viper"
)

func main() {
	common.InitLogger(viper.GetString("log.level"), viper.GetString("log.filename"))
	defer common.Logger.Sync()

	ipServer := service.NewIPService()
	updator := service.NewDnsUpdator()
	var notifyServer service.Notify
	enableNotify := viper.GetBool("notification.enable")
	if enableNotify {
		if viper.GetBool("notification.mail.enable") {
			notifyServer = service.NewMailService("Noip-Updator", 30) // 空闲时间30s
		}
	}

	server := server.NewServer(ipServer, updator, notifyServer)
	server.MonPublicIPChange()
}

func InitConfig() {
	// workDir, _ := os.Getwd()
	dir, _ := os.Executable()
	workDir := filepath.Dir(dir)
	viper.SetConfigName("application")
	viper.SetConfigType("yml")
	viper.AddConfigPath(filepath.Join(workDir, "config/"))
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}
}

func init() {
	InitConfig()
}
